﻿using System;
using System.Configuration;
using System.Xml;
using System.IO;
using System.Web;

namespace Phoenix.Common.Helper
{
	public class ConfigHelper
	{
		#region 私有
		private XmlDocument doc;
		private string _fileName;
		private string _filePath;
		private bool _ingoreNullNode = false;
		#endregion

		#region Properties
		/// <summary>
		/// 文件名
		/// </summary>
		public string FileName
		{
			get { return _fileName; }
			set { _fileName = value; }
		}

		/// <summary>
		/// 文件路径
		/// </summary>
		public string FilePath
		{
			get { return _filePath; }
			set { _filePath = value; }
		}

		/// <summary>
		/// 是否忽略空节点
		/// </summary>
		public bool IngoreNullNode
		{
			get { return _ingoreNullNode; }
			set { _ingoreNullNode = value; }
		}
		#endregion

		#region ConfigHelper
		/// <summary>
		/// 初始化函数
		/// </summary>
		/// <param name="fileName">文件名</param>
		public ConfigHelper(string fileName)
		{
			this._fileName = fileName;
		}

		/// <summary>
		/// 初始化函数
		/// </summary>
		/// <param name="fileName">文件名</param>
		/// <param name="ingoreNullNode">是否忽略空节点</param>
		public ConfigHelper(string fileName, bool ingoreNullNode)
		{
			this._fileName = fileName;
			this._ingoreNullNode = ingoreNullNode;
		}
		#endregion

		#region 增删改查

		#region Load
		/// <summary>
		/// 载入配置文件
		/// </summary>
		public void Load()
		{
			if (this._fileName != null)
			{
				this._filePath = HttpContext.Current.Server.MapPath(this._fileName);
			}
			else if (this._filePath != null)
			{
				this._fileName = Path.GetFileName(this._filePath);
			}
			else
			{
				throw new ArgumentException("未指定配置文件");
			}

			doc = new XmlDocument();

			try
			{
				doc.Load(this._filePath);
			}
			catch
			{
				throw new ArgumentException(string.Format("配置文件 \"{0}\" 不存在", _fileName));
			}
		}
		#endregion

		#region Save
		/// <summary>
		/// 保存配置文件
		/// </summary>
		public void Save()
		{
			try
			{
				doc.Save(_filePath);
			}
			catch (Exception e)
			{

				throw new ArgumentException(string.Format("配置文件 \"{0}\" 保存失败", this._fileName) + e.Message);
			}
		}
		#endregion

		#region GetNode
		private XmlNode GetNode(string key)
		{
			string xPath = string.Format("/Settings/Add[@Key='{0}']", key);
			XmlNode node = doc.SelectSingleNode(xPath);
			return node;
		}
		#endregion

		#region Select
		/// <summary>
		/// 从配置文件中查询对应键的值
		/// </summary>
		/// <param name="key">关键字</param>
		/// <returns>查询到的值</returns>
		public string Select(string key)
		{
			XmlNode node = this.GetNode(key);

			if (node == null)
			{
				if (this._ingoreNullNode)
				{
					return "";
				}
				else
				{
					throw new ArgumentException(string.Format("配置节点 \"{0}\" 不存在", key));
				}
			}

			return node.Attributes["Value"].Value;
		}
		#endregion

		#region Insert
		/// <summary>
		/// 插入配置内容
		/// </summary>
		/// <param name="key">插入关键字</param>
		/// <param name="value">插入值</param>
		public void Insert(string key, string value)
		{
			XmlNode node = this.GetNode(key);

			if (node != null)
			{
				if (this._ingoreNullNode)
				{
					this.Update(key, value);
					return;
				}
				else
				{
					throw new ArgumentException(string.Format("配置节点 \"{0}\" 已存在", key));
				}
			}

			node = doc.CreateElement("Add");
			XmlAttribute attKey = doc.CreateAttribute("Key");
			XmlAttribute attValue = doc.CreateAttribute("Value");
			attKey.Value = key;
			attValue.Value = value;
			node.Attributes.Append(attKey);
			node.Attributes.Append(attValue);
			doc.DocumentElement.AppendChild(node);
		}
		#endregion

		#region Update
		/// <summary>
		/// 更新配置文件对应关键字的值
		/// </summary>
		/// <param name="key">更新关键字</param>
		/// <param name="value">更新的值</param>
		public void Update(string key, string value)
		{
			XmlNode node = this.GetNode(key);

			if (node == null)
			{
				if (this._ingoreNullNode)
				{
					this.Insert(key, value);
					return;
				}
				else
				{
					throw new ArgumentException(string.Format("配置节点 \"{0}\" 不存在", key));
				}
			}

			node.Attributes["Value"].Value = value;
		}

		/// <summary>
		/// 更新配置文件对应关键字的值
		/// </summary>
		/// <param name="key">更新关键字</param>
		/// <param name="value">更新的值</param>
		/// <param name="create">不存在是是否需要创建</param>
		public void Update(string key, string value, bool create)
		{
			XmlNode node = this.GetNode(key);

			if (node == null)
			{
				if (create)
				{
					this.Insert(key, value);
				}
				else
				{
					throw new ArgumentException(string.Format("配置节点 \"{0}\" 不存在", key));
				}
			}
			else
			{
				node.Attributes["Value"].Value = value;
			}
		}
		#endregion

		#region Delete
		/// <summary>
		/// 删除配置文件对应关键字
		/// </summary>
		/// <param name="key">关键字</param>
		public void Delete(string key)
		{
			XmlNode node = this.GetNode(key);

			if (node != null)
			{
				doc.DocumentElement.RemoveChild(node);
			}
		}
		#endregion

		#endregion

		/// <summary>
		/// 读取配置节点信息
		/// </summary>
		/// <param name="name">节点名称</param>
		/// <returns></returns>
		public static string GetValue(string name)
		{
			string strValue = ConfigurationManager.AppSettings[name].ToString();
			if (String.IsNullOrEmpty(strValue))
			{
				throw new ArgumentNullException(string.Format("AppSetting配置节找不到key为{0}的配置信息", name));
			}
			return strValue;

		}

		/// <summary>
		/// 从AppSetting中获取Value值。
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		private static string getValue(string name)
		{
			string kv = System.Configuration.ConfigurationManager.AppSettings[name];
			if (string.IsNullOrEmpty(kv))
			{
				throw new ArgumentNullException(string.Format("AppSetting配置节找不到key为{0}的配置信息", name));
			}
			return kv;
		}

		#region 预警服务邮件配置系数
		/// <summary>
		/// 预警服务邮件配置系数
		/// </summary>
		public class SendMail
		{
			static SendMail()
			{
				_MailFrom = getValue("MailFrom");
				_SMTPServer = getValue("SMTPServer");
				_SMTPUsername = getValue("SMTPUsername");
				_SMTPPassword = getValue("SMTPPassword");
			}

			public static string MailFrom
			{
				get { return _MailFrom; }
			}
			public static string SMTPServer
			{
				get { return _SMTPServer; }
			}
			public static string SMTPUsername
			{
				get { return _SMTPUsername; }
			}
			public static string SMTPPassword
			{
				get { return _SMTPPassword; }
			}

			private static string _MailFrom;
			private static string _SMTPServer;
			private static string _SMTPUsername;
			private static string _SMTPPassword;
		}
		#endregion


		#region 后台管理网站配置文件读取类
		/// <summary>
		/// 后台管理网站配置文件读取类
		/// </summary>
		public class Admin
		{
			static Admin()
			{
				_imageServerUrl = getValue("ImageServerUrl");
				_loanFrontImageServerPath = getValue("LoanFrontImageServerPath");
				_adminInterfaceUrl = getValue("AdminInterfaceUrl");
				_adminUrl = getValue("AdminUrl");
				_adminLogoutUrl = getValue("AdminLogoutUrl");
				_newAdminLoginUrl = getValue("NewAdminLoginUrl");
				_newAdminLogoutUrl = getValue("NewAdminLogoutUrl");
				_queryAdminUserUrl = getValue("QueryAdminUserUrl");
			}


			#region Properties

			/// <summary>
			/// 图片服务器URL地址。
			/// 例如：http://loan-image.esunny.com/
			/// </summary>
			public static string ImageServerUrl
			{
				get
				{
					return _imageServerUrl;
				}
			}

			/// <summary>
			/// 融资用户前台图片服务器物理路径
			/// 例如：E:\Image\Upload\
			/// </summary>
			public static string LoanFrontImageServerPath
			{
				get
				{
					return _loanFrontImageServerPath;
				}
			}

			/// <summary>
			/// 管理网站接口URL地址.
			/// 例如:http://admin.eunny.com/LoginService.aspx
			/// </summary>
			public static string AdminInterfaceUrl
			{
				get
				{
					return _adminInterfaceUrl;
				}
			}

			/// <summary>
			/// 管理网站退出登录URL地址.
			/// </summary>
			public static string AdminLogoutUrl
			{
				get
				{
					return _adminLogoutUrl;
				}
			}

			/// <summary>
			/// 管理网站URL地址.
			/// 例如:http://admin.eunny.com/
			/// </summary>
			public static string AdminUrl
			{
				get
				{
					return _adminUrl;
				}
			}

			/// <summary>
			/// 新管理网站登录URL地址.
			/// 例如:http://admin.eunny.com/login.aspx
			/// </summary>
			public static string NewAdminLoginUrl
			{
				get
				{
					return _newAdminLoginUrl;
				}
			}

			/// <summary>
			/// 新管理网站退出URL地址.
			/// 例如:http://admin.eunny.com/logout.aspx
			/// </summary>
			public static string NewAdminLogoutUrl
			{
				get
				{
					return _newAdminLogoutUrl;
				}
			}

			/// <summary>
			/// 新接口，查询运营后台用户列表URL
			/// 例如:http://bops.esunny.com:8007/login/sz_query_users.htm
			/// </summary>
			public static string QueryAdminUserUrl
			{
				get
				{
					return _queryAdminUserUrl;
				}
			}

			#endregion


			#region Private Member

			private static string _imageServerUrl;
			private static string _loanFrontImageServerPath;
			private static string _adminInterfaceUrl;
			private static string _adminUrl;
			private static string _adminLogoutUrl;
			private static string _newAdminLoginUrl;
			private static string _newAdminLogoutUrl;
			private static string _queryAdminUserUrl;

			#endregion
		}
		#endregion


		#region 前台网站配置文件读取类
		/// <summary>
		/// 前台网站配置文件读取类
		/// </summary>
		public class Common
		{

			#region Properties


			public static string MstContact
			{
				get
				{
					return getValue("MstContact");
				}
			}

			public static string StaticsUrl
			{
				get
				{
					return getValue("StaticsUrl");
				}
			}

			public static string InfoPubOfferCatchCnt
			{
				get
				{
					return getValue("InfoPubOfferCatchCnt");
				}
			}


			public static string StaticResourceVersion
			{
				get
				{
					return getValue("StaticResourceVersion");
				}
			}


			public static string MstLoginInfo
			{
				get
				{
					return getValue("MstLoginInfo");
				}
			}

			public static string MstLogin
			{
				get
				{
					return getValue("MstLogin");
				}
			}

			public static string MstLogout
			{
				get
				{
					return getValue("MstLogout");
				}
			}

			public static string MstInbox
			{
				get
				{
					return getValue("MstInbox");
				}
			}

			public static string ListTemplates
			{
				get
				{
					return getValue("ListTemplates");
				}
			}

			public static string server_Offer_UploadImg
			{
				get
				{
					return getValue("server_Offer_UploadImg");
				}
			}

			#endregion


		}

		#endregion






	}

}
